package com.bella.api.system.service.impl;

import com.bella.api.system.dao.mapper.ext.AuthRoleExtendMapper;
import com.bella.api.system.dao.mapper.ext.AuthRoleMenuExtendMapper;
import com.bella.api.system.dao.mapper.ext.AuthUserRoleExtendMapper;
import com.bella.api.system.dao.model.AuthRole;
import com.bella.api.system.dao.model.AuthRoleMenu;
import com.bella.api.system.dto.role.RoleDto;
import com.bella.api.system.dto.role.RoleQueryDto;
import com.bella.api.system.service.RoleService;
import com.bella.api.utils.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zsq
 * @create 2020-05-13-17:17
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    AuthRoleExtendMapper authRoleExtendMapper;
    @Autowired
    AuthRoleMenuExtendMapper authRoleMenuExtendMapper;
    @Autowired
    AuthUserRoleExtendMapper authUserRoleExtendMapper;

    /**
     * 角色列表
     *
     * @param roleQueryDto
     * @return
     */
    @Override
    public PageResult getRoleList(RoleQueryDto roleQueryDto) {
        PageHelper.startPage(roleQueryDto.getCurrPage(), roleQueryDto.getPageSize());
        List<AuthRole> adminList = authRoleExtendMapper.getRoleList(roleQueryDto.getRoleName());
        PageInfo pageInfo = new PageInfo<>(adminList);

        PageResult result = new PageResult<>();
        result.setTotalPage(pageInfo.getPages());
        result.setTotalCount(pageInfo.getTotal());
        result.setCurrPage(pageInfo.getPages());
        result.setPageSize(pageInfo.getPageSize());
        result.setList(adminList);
        return result;
    }

    /**
     * 获取所有角色
     *
     * @return
     */
    @Override
    public List<AuthRole> getRoleList() {
        return authRoleExtendMapper.getRoleList(null);
    }

    /**
     * 角色详情
     *
     * @param id
     * @return
     */
    @Override
    public RoleDto roleDetail(Integer id) {
        AuthRole authRole = authRoleExtendMapper.selectByPrimaryKey(id);
        RoleDto roleDto = new RoleDto();
        BeanUtils.copyProperties(authRole,roleDto);
        roleDto.setMenuList(authRoleMenuExtendMapper.getRoleMenuList(id).stream().map(AuthRoleMenu::getMenuId).collect(Collectors.toList()));
        return roleDto;
    }

    /**
     * 新增角色
     *
     * @param addRoleDto
     */
    @Override
    @Transactional
    public void addRole(RoleDto addRoleDto) {
        AuthRole authRole = new AuthRole();
        BeanUtils.copyProperties(addRoleDto, authRole);
        authRole.setStatus(1);
        authRoleExtendMapper.insertSelective(authRole);

        if(null != addRoleDto.getMenuList()){
            batchAddRoleMenu(authRole.getId(), addRoleDto.getMenuList());
        }
    }

    /**
     * 修改角色
     *
     * @param addRoleDto
     */
    @Override
    @Transactional
    public void updRole(RoleDto addRoleDto) {
        AuthRole authRole = new AuthRole();
        BeanUtils.copyProperties(addRoleDto, authRole);
        authRoleExtendMapper.updateByPrimaryKeySelective(authRole);

        authRoleMenuExtendMapper.deleteRoleMenuByRoleId(addRoleDto.getId());

        if(null != addRoleDto.getMenuList()){
            batchAddRoleMenu(authRole.getId(), addRoleDto.getMenuList());
        }
    }

    /**
     * 删除角色
     *
     * @param addRoleDto
     */
    @Override
    public void deleteRole(RoleDto addRoleDto) {
        AuthRole authRole = new AuthRole();
        authRole.setId(addRoleDto.getId());
        authRole.setStatus(0);
        authRoleExtendMapper.updateByPrimaryKeySelective(authRole);

        authRoleMenuExtendMapper.deleteRoleMenuByRoleId(addRoleDto.getId());

        authUserRoleExtendMapper.deleteByUserId(addRoleDto.getId());
    }

    private void batchAddRoleMenu(Integer roleId, List<Integer> menuIdList) {
        List<AuthRoleMenu> authRoleMenuList = new ArrayList<>();
        AuthRoleMenu authRoleMenu = null;
        for (Integer menuId : menuIdList) {
            authRoleMenu = new AuthRoleMenu();
            authRoleMenu.setRoleId(roleId);
            authRoleMenu.setMenuId(menuId);
            authRoleMenuList.add(authRoleMenu);
        }
        if (authRoleMenuList.size() > 0) {
            authRoleMenuExtendMapper.batchAdd(authRoleMenuList);
        }
    }
}
